﻿// -----------------------------------------------------------------------
// <copyright file="TaskManager.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Metal.Tasks
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;
    using Metal.Config;
    using System.Timers;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class TaskManager
    {
        /// <summary>Used to load xml from file</summary>
        private XmlDocument xmlDocument = new XmlDocument();

        /// <summary>Used to delay the task loading</summary>
        private Timer delayTimer;

        /// <summary>
        /// Initializes a new instance of the TaskManager class.
        /// </summary>
        public TaskManager()
        {
            // Load theme config
            this.xmlDocument.Load(Paths.ConfigFolder + "\\Tasks.xml");
            delayTimer = new Timer();
            delayTimer.Interval = Convert.ToInt32(this.xmlDocument.SelectSingleNode("Tasks").Attributes["startDelay"].Value);
            delayTimer.Elapsed += new ElapsedEventHandler(delayTimer_Elapsed);
        }

        void delayTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            this.DelayedTaskLoader();
            delayTimer.Stop();
        }

        /// <summary>
        /// Loads tasks defined in tasks.xml config file, and executes the initialization method defined in "callMethod" attribute
        /// </summary>
        public void LoadTasks()
        {
            try
            {
                delayTimer.Start();
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        /// <summary>
        /// Delayed task loader
        /// </summary>
        private void DelayedTaskLoader()
        {
            try
            {
                XmlNodeList tasks = this.xmlDocument.SelectNodes("Tasks")[0].ChildNodes;
                foreach (XmlNode task in tasks)
                {
                    // Create instance of the specified class
                    System.Reflection.Assembly assembly = System.Reflection.Assembly.Load(task.Attributes["namespace"].Value.ToString());
                    Type t = assembly.GetType(task.Attributes["className"].Value.ToString());
                    object reflectionObject = (object)Activator.CreateInstance(t);

                    // Call the method defined in "callMethod" attribute (if any). You can do stuff in the constructor, or you can call what method you want.
                    if (task.Attributes["callMethod"].Value != null)
                    {
                        System.Reflection.MethodInfo theMethod = t.GetMethod(task.Attributes["callMethod"].Value.ToString(), System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
                        if (theMethod != null)
                        {
                            theMethod.Invoke(reflectionObject, null);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }
    }
}
